home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / turbovis / numinp2.zip / NUMINPUT.PAS < prev    next >
Pascal/Delphi Source File  |  1993-05-25  |  12KB  |  404 lines

  1. unit      NumInput;
  2.  
  3. { ************************************************************************** }
  4. { * Numeric Input for Turbo Vision version 2                               * }
  5. { * ----------------------------------------                               * }
  6. { *                                                                        * }
  7. { * Offers you the possibility of Byte-, Word- Integer and                 * }
  8. { *                                                 Longint-inputlines.    * }
  9. { *                                                                        * }
  10. { * Byte- and Word-inputlines are range-checked at the moment the data     * }
  11. { * is entered, so no validation-checking afterwards is needed.            * }
  12. { *                                                                        * }
  13. { * Warning: Let the length of your inputview be long enough, because      * }
  14. { *          the scrolling of this unit is not that good. (In my           * }
  15. { *          opinion not necessary)                                        * }
  16. { *                                                                        * }
  17. { * Author: Edwin Groothuis (S89405079@hsepm1.hse.nl or 2:284/205.1)       * }
  18. { * How to newer versions:                                                 * }
  19. { *                internet-users:                                         * }
  20. { *                 garbo.uwasa.fi:/pc/turbovis/numinp#.zip                * }
  21. { *                 oak.oakland.edu:/pub/msdos/turbovis/numinp#.zip        * }
  22. { *                fidonet-users:                                          * }
  23. { *                 freq NUMINP at 2:284/205 (+31-40-550352)               * }
  24. { *                 download NUMINP#.ZIP from 2:284/205 area: TurboPascal  * }
  25. { *                                                                        * }
  26. { * The source is hereby Public Domain, no warrenties are made, feel free  * }
  27. { * to modify or update. Please send bugs reports/fixes to me.             * }
  28. { *                                                                        * }
  29. { ************************************************************************** }
  30.  
  31. {$F+}
  32. interface
  33.  
  34. uses      dialogs,drivers,objects;
  35.  
  36. type
  37. { A ByteInputLine gives you a byte. Can be in the range of 0..255 }
  38.           PByteInputLine=^TByteInputLine;
  39.           TByteInputLine=object(TInputLine)
  40.                            constructor Init(var Bounds:TRect);
  41.                            procedure HandleEvent(var E:TEvent);virtual;
  42.                            function  DataSize:Word;virtual;
  43.                            procedure GetData(var Rec);virtual;
  44.                            procedure SetData(var Rec);virtual;
  45.                         end;
  46. { A WordInputLine gives you a word. Can be in the range of 0..65535 }
  47.           PWordInputLine=^TWordInputLine;
  48.           TWordInputLine=object(TInputLine)
  49.                            constructor Init(var Bounds:TRect);
  50.                            procedure HandleEvent(var E:TEvent);virtual;
  51.                            function  DataSize:Word;virtual;
  52.                            procedure GetData(var Rec);virtual;
  53.                            procedure SetData(var Rec);virtual;
  54.                         end;
  55. { A IntInputLine gives you a integer. Can be in the range of -32768..32767 }
  56.           PIntInputLine=^TIntInputLine;
  57.           TIntInputLine=object(TInputLine)
  58.                            constructor Init(var Bounds:TRect);
  59.                            procedure HandleEvent(var E:TEvent);virtual;
  60.                            function  DataSize:Word;virtual;
  61.                            procedure GetData(var Rec);virtual;
  62.                            procedure SetData(var Rec);virtual;
  63.                         end;
  64. { A LongInputLine gives you a longint. Can be in the range of -2147483648..2147483647 }
  65.           PLongintInputLine=^TLongintInputLine;
  66.           TLongintInputLine=object(TInputLine)
  67.                            constructor Init(var Bounds:TRect);
  68.                            procedure HandleEvent(var E:TEvent);virtual;
  69.                            function  DataSize:Word;virtual;
  70.                            procedure GetData(var Rec);virtual;
  71.                            procedure SetData(var Rec);virtual;
  72.                         end;
  73.  
  74.  
  75. implementation
  76.  
  77.  
  78. {****************************************************************************}
  79.  
  80.  
  81.  
  82.  
  83. {****************************************************************************}
  84.  
  85.  
  86. constructor TByteInputLine.Init(var Bounds:TRect);
  87. begin
  88.   Inherited Init(Bounds,3);
  89. end;
  90.  
  91.  
  92.  
  93. procedure TByteInputLine.HandleEvent(var E:TEvent);
  94. var       s:string;
  95.           l:longint;
  96.           code:integer;
  97. begin
  98.   inherited HandleEvent(e);
  99.   s:=Data^;
  100.   val(s,l,code);
  101.   if code=0 then { everything seems to be OK }
  102.   begin
  103.     if l>255 then { It was meant to be a byte, so remove the
  104.                     last inputted character }
  105.     begin
  106.       system.Delete(s,CurPos,1);
  107.       Dec(CurPos);
  108.     end else begin
  109.       if pos(' ',s)<>0 then { yes, it was entered }
  110.       begin
  111.         system.delete(s,pos(' ',s),1);
  112.         Dec(CurPos);
  113.       end else begin
  114.         if s[1]='0' then { don't let something begin with a 0! }
  115.         begin
  116.           system.delete(s,1,1);
  117.           Dec(CurPos);
  118.         end else begin { everything IS ok }
  119.           { PARTY! }
  120.         end;
  121.       end;
  122.     end;
  123.   end else begin { ohoh... integer overflow or non-numericalcharacter typed
  124.                    just kill the last inputted character of the string }
  125.     if s[0]<>#0 then { but don't make it smaller than it can be }
  126.     begin
  127.       system.Delete(s,CurPos,1);
  128.       Dec(CurPos);
  129.     end;
  130.   end;
  131.   Data^:=s;
  132.   inherited Draw;
  133. end;
  134.  
  135.  
  136.  
  137. function  TByteInputLine.DataSize:Word;
  138. begin
  139.   DataSize:=1;
  140. end;
  141. procedure TByteInputLine.SetData(var Rec);
  142. begin
  143.   str(Byte(Rec),Data^);
  144. end;
  145. procedure TByteInputLine.GetData(var Rec);
  146. var       code:integer;
  147. begin
  148.   val(Data^,Byte(Rec),code);
  149. end;
  150.  
  151.  
  152. {****************************************************************************}
  153.  
  154.  
  155. constructor TWordInputLine.Init(var Bounds:TRect);
  156. begin
  157.   Inherited Init(Bounds,5);
  158. end;
  159.  
  160.  
  161.  
  162. procedure TWordInputLine.HandleEvent(var E:TEvent);
  163. var       s:string;
  164.           l:longint;
  165.           code:integer;
  166. begin
  167.   inherited HandleEvent(e);
  168.   s:=Data^;
  169.   val(s,l,code);
  170.   if code=0 then { everything seems to be OK }
  171.   begin
  172.     if l>longint($FFFF) then { It was meant to be a word, so remove the
  173.                                last inputted character }
  174.     begin
  175.       system.Delete(s,CurPos,1);
  176.       Dec(CurPos);
  177.     end else begin
  178.       if pos(' ',s)<>0 then { yes, it was entered }
  179.       begin
  180.         system.delete(s,pos(' ',s),1);
  181.         Dec(CurPos);
  182.       end else begin
  183.         if s[1]='0' then { don't let something begin with a 0! }
  184.         begin
  185.           system.delete(s,1,1);
  186.           Dec(CurPos);
  187.         end else begin { everything IS ok }
  188.           { PARTY! }
  189.         end;
  190.       end;
  191.     end;
  192.   end else begin { ohoh... integer overflow or non-numericalcharacter typed
  193.                    just kill the last inputted character of the string }
  194.     if s[0]<>#0 then { but don't make it smaller than it can be }
  195.     begin
  196.       system.Delete(s,CurPos,1);
  197.       Dec(CurPos);
  198.     end;
  199.   end;
  200.   Data^:=s;
  201.   inherited Draw;
  202. end;
  203.  
  204.  
  205.  
  206. function  TWordInputLine.DataSize:Word;
  207. begin
  208.   DataSize:=2;
  209. end;
  210. procedure TWordInputLine.SetData(var Rec);
  211. begin
  212.   str(Word(Rec),Data^);
  213. end;
  214. procedure TWordInputLine.GetData(var Rec);
  215. var       code:integer;
  216. begin
  217.   val(Data^,Word(Rec),code);
  218. end;
  219.  
  220.  
  221. {****************************************************************************}
  222.  
  223.  
  224. constructor TIntInputLine.Init(var Bounds:TRect);
  225. begin
  226.   Inherited Init(Bounds,6);
  227. end;
  228.  
  229.  
  230.  
  231. procedure TIntInputLine.HandleEvent(var E:TEvent);
  232. var       s,t:string;
  233.           l:longint;
  234.           code:integer;
  235.           len:byte;
  236. begin
  237.   len:=length(Data^);
  238.   inherited HandleEvent(e);
  239.   s:=Data^;
  240.   if e.charcode='-' then
  241.   begin
  242.     t:=copy(s,pos('-',s)+1,length